package com.ruoyi.web.controller.bl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.bl.domain.BlOrder;
import com.ruoyi.bl.service.IBlOrderService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bl.domain.BlDeliveryDoc;
import com.ruoyi.bl.service.IBlDeliveryDocService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 发货和配送单Controller
 * 
 * @author ruoyi
 * @date 2024-12-07
 */
@RestController
@RequestMapping("/bl/deliveryDoc")
public class BlDeliveryDocController extends BaseController
{
    @Autowired
    private IBlDeliveryDocService blDeliveryDocService;

    @Autowired
    private IBlOrderService blOrderService;

    /**
     * 查询发货和配送单列表
     */
    @PreAuthorize("@ss.hasPermi('bl:deliveryDoc:list')")
    @GetMapping("/list")
    public TableDataInfo list(BlDeliveryDoc blDeliveryDoc)
    {
        startPage();
        List<BlDeliveryDoc> list = blDeliveryDocService.selectBlDeliveryDocList(blDeliveryDoc);
        return getDataTable(list);
    }

    /**
     * 导出发货和配送单列表
     */
    @PreAuthorize("@ss.hasPermi('bl:deliveryDoc:export')")
    @Log(title = "发货和配送单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BlDeliveryDoc blDeliveryDoc)
    {
        List<BlDeliveryDoc> list = blDeliveryDocService.selectBlDeliveryDocList(blDeliveryDoc);
        ExcelUtil<BlDeliveryDoc> util = new ExcelUtil<BlDeliveryDoc>(BlDeliveryDoc.class);
        util.exportExcel(response, list, "发货和配送单数据");
    }

    /**
     * 获取发货和配送单详细信息
     */
    @PreAuthorize("@ss.hasPermi('bl:deliveryDoc:query')")
    @GetMapping(value = "/{deliveryId}")
    public AjaxResult getInfo(@PathVariable("deliveryId") Long deliveryId)
    {
        return success(blDeliveryDocService.selectBlDeliveryDocByDeliveryId(deliveryId));
    }

    /**
     * 新增发货和配送单
     */
    @PreAuthorize("@ss.hasPermi('bl:deliveryDoc:add')")
    @Log(title = "发货和配送单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BlDeliveryDoc blDeliveryDoc)
    {
        return toAjax(blDeliveryDocService.insertBlDeliveryDoc(blDeliveryDoc));
    }

    /**
     * 修改发货和配送单
     */
    @PreAuthorize("@ss.hasPermi('bl:deliveryDoc:edit')")
    @Log(title = "发货和配送单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlDeliveryDoc blDeliveryDoc)
    {
        return toAjax(blDeliveryDocService.updateBlDeliveryDoc(blDeliveryDoc));
    }

    /**
     * 删除发货和配送单
     */
    @PreAuthorize("@ss.hasPermi('bl:deliveryDoc:remove')")
    @Log(title = "发货和配送单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deliveryIds}")
    public AjaxResult remove(@PathVariable Long[] deliveryIds)
    {
        return toAjax(blDeliveryDocService.deleteBlDeliveryDocByDeliveryIds(deliveryIds));
    }

    /**
     * 查询发货和配送单列表
     */
    @PreAuthorize("@ss.hasPermi('bl:deliveryDoc:list')")
    @GetMapping("/getDeliveryDoc")
    public AjaxResult getDeliveryDoc(BlDeliveryDoc blDeliveryDoc)
    {
        blDeliveryDoc.setDeptId(getDeptId());
        blDeliveryDoc.setPsuserId(getUserId());
        if(blDeliveryDoc.getPsStatus() == null || blDeliveryDoc.getPsStatus().equals("")){
            blDeliveryDoc.setPsStatusParams("0,1,2");
        }
        List<BlDeliveryDoc> list = blDeliveryDocService.selectBlDeliveryDocList(blDeliveryDoc);
        return success(list);
    }

    /**
     * 配送接单
     */
    @PreAuthorize("@ss.hasPermi('bl:deliveryDoc:edit')")
    @Log(title = "发货和配送单", businessType = BusinessType.UPDATE)
    @PutMapping("/doDeliveryReceive")
    public AjaxResult doDeliveryReceive(@RequestBody BlDeliveryDoc blDeliveryDoc)
    {
        if(blDeliveryDoc.getDeliveryId() == null || blDeliveryDoc.getDeliveryId().equals("")){
            return error("配送单号不能为空!");
        }
        long deliveryId = blDeliveryDoc.getDeliveryId();
        BlDeliveryDoc bddTmp = blDeliveryDocService.selectBlDeliveryDocByDeliveryId(deliveryId);
        long psStatus = bddTmp.getPsStatus();
        if(psStatus !=0){
            return error("配送状态非待接单状态!");
        }
        if(bddTmp.getOrderId() == null || bddTmp.getOrderId().equals("")){
            return error("订单号为空!");
        }
        blDeliveryDoc.setPsStatus((long)1);

        long orderId = bddTmp.getOrderId();
        BlOrder boTmp =blOrderService.selectBlOrderByOrderId(orderId);
        int orderStatus = boTmp.getOrderStatus();
        if(orderStatus !=0){
            return error("订单状态异常!");
        }
        BlOrder blOrder = new BlOrder();
        blOrder.setOrderId(orderId);
        blOrder.setDeliveryStatus(1);

        blDeliveryDocService.updateBlDeliveryDoc(blDeliveryDoc);
        return toAjax(blOrderService.updateBlOrder(blOrder));
    }

    /**
     * 配送中
     */
    @PreAuthorize("@ss.hasPermi('bl:deliveryDoc:edit')")
    @Log(title = "发货和配送单", businessType = BusinessType.UPDATE)
    @PutMapping("/doDeliveryProgress")
    public AjaxResult doDeliveryProgress(@RequestBody BlDeliveryDoc blDeliveryDoc) throws ParseException
    {
        if(blDeliveryDoc.getDeliveryId() == null || blDeliveryDoc.getDeliveryId().equals("")){
            return error("配送单号不能为空!");
        }
        long deliveryId = blDeliveryDoc.getDeliveryId();
        BlDeliveryDoc bddTmp = blDeliveryDocService.selectBlDeliveryDocByDeliveryId(deliveryId);
        long psStatus = bddTmp.getPsStatus();
        if(psStatus !=1){
            return error("配送状态非接单状态!");
        }
        if(bddTmp.getOrderId() == null || bddTmp.getOrderId().equals("")){
            return error("订单号为空!");
        }
        String dateString = DateUtils.getTime();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = formatter.parse(dateString);
        blDeliveryDoc.setPsStarttime(startTime);
        blDeliveryDoc.setPsStatus((long)2);

        long orderId = bddTmp.getOrderId();
        BlOrder boTmp =blOrderService.selectBlOrderByOrderId(orderId);
        int orderStatus = boTmp.getOrderStatus();
        if(orderStatus !=0){
            return error("订单状态异常!");
        }
        BlOrder blOrder = new BlOrder();
        blOrder.setOrderId(orderId);
        blOrder.setShippingStatus(1);
        blOrder.setDeliveryStatus(2);
        blOrder.setDeliveryTime(startTime);
        blDeliveryDocService.updateBlDeliveryDoc(blDeliveryDoc);
        return toAjax(blOrderService.updateBlOrder(blOrder));
    }

    /**
     * 配送完成
     */
    @PreAuthorize("@ss.hasPermi('bl:deliveryDoc:edit')")
    @Log(title = "发货和配送单", businessType = BusinessType.UPDATE)
    @PutMapping("/doDeliveryFinish")
    public AjaxResult doDeliveryFinish(@RequestBody BlDeliveryDoc blDeliveryDoc) throws ParseException {
        if(blDeliveryDoc.getDeliveryId() == null || blDeliveryDoc.getDeliveryId().equals("")){
            return error("配送单号不能为空!");
        }
        long deliveryId = blDeliveryDoc.getDeliveryId();
        BlDeliveryDoc bddTmp = blDeliveryDocService.selectBlDeliveryDocByDeliveryId(deliveryId);
        long psStatus = bddTmp.getPsStatus();
        if(psStatus !=2){
            return error("配送状态非配送中!");
        }
        if(bddTmp.getOrderId() == null || bddTmp.getOrderId().equals("")){
            return error("订单号为空!");
        }
        String dateString = DateUtils.getTime();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date arriveTime = formatter.parse(dateString);
        blDeliveryDoc.setPsEndtime(arriveTime);
        blDeliveryDoc.setPsStatus((long)4);

        long orderId = bddTmp.getOrderId();
        BlOrder boTmp =blOrderService.selectBlOrderByOrderId(orderId);
        int orderStatus = boTmp.getOrderStatus();
        if(orderStatus !=0){
            return error("订单状态异常!");
        }
        BlOrder blOrder = new BlOrder();
        blOrder.setOrderId(orderId);
        blOrder.setArriveTime(arriveTime);
        blOrder.setOrderStatus(4);
        blOrder.setShippingStatus(1);
        blOrder.setDeliveryStatus(4);
        blDeliveryDocService.updateBlDeliveryDoc(blDeliveryDoc);
        return toAjax(blOrderService.updateBlOrder(blOrder));
    }
}
